<!-- 供应商/经纪table -->
<template>
  <div>
    <search :searchForm="searchForm" @searchList="searchList" @add="add" @push="push" />
    <table-list
      :tableData="tableData"
      :tableLabels="tableLabels"
      :tableProps="tableProps"
      :tableChangeBtns="tableChangeBtns"
      :activeBtn="tableBtnRule"
      :hasSera="true"
      :tableFuns="tableFuns"
    />

    <el-pagination
      layout="total, sizes, prev, pager, next, jumper"
      style="margin-top:30px"
      :current-page.sync="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :total="totalSize"
    />
  </div>
</template>
<script>
import api from "@/api/apiPath";
import tableList from "../common/tableList";
import topBar from "../common/topChangeBar";
import search from "./searchForm";
export default {
  components: { "table-list": tableList, "top-bar": topBar, search },
  name: "changeChannelOrAgent",
  data() {
    return {
      tableProps: [
        "companyCity",
        "companyName",
        "id",
        "statusName",
        "limits",
        "taxrate",
        "storesCount",
        "brokerCount",
        "userName",
        "createTime",
        "companyRepresen",
        "changeBtn"
      ],
      tableLabels: [
        "城市",
        "公司名称",
        "公司ID",
        "审核状态",
        "权限",
        "税率",
        "经纪门店数量",
        "经纪人数量",
        "新建人",
        "新建时间",
        "是否可使用金融产品",
        "操作"
      ],
      tableFuns: {
        storesCount: this.toBrokerageStore,
        brokerCount: this.toBorkerPage
      },
      tableChangeBtns: [
        { label: "发起审批", type: "approval", fn: this.initApproval },
        { label: "编辑", type: "edit", fn: this.viewList },
        { label: "详情", type: "view", fn: this.viewList },
        { label: "删除", type: "dele", fn: this.deleteItem }
      ],
      tableData: [],
      pageNum: 1,
      pageSize: 10, //每页几条
      totalSize: 0,

      searchForm: {
        companyCityCode: "",
        companyName: "",
        strTime: "",
        endTime: "",
        status: "",
        house: "",
        insurance: "",
        property: ""
      }
    };
  },
  methods: {
    toBorkerPage(index, row) {
      this.$router.push({
        path: `/viewBroker`,
        query: { companyName: row.companyName }
      });
    },
    toBrokerageStore(index, row) {
      this.$router.push({
        path: "/viewBrokerageStore",
        query: { companyName: row.companyName }
      });
    },
    handleClick() {},
    //新增查看编辑
    viewList(type, idx) {
      let insideId = this.tableData[idx].id;
      let sort = this.tableData[idx].sort;
      this.$router.push({
        path: "/changeChannelOrAgent",
        query: { pageType: type, insideId: insideId, sort: sort }
      });
    },
    //发起审批
    initApproval(type, idx) {
      this.$confirm(`确定要发起审批吗？`, "", {
        confirmButtonText: "确定",
        cancelButtonText: "取消"
      })
        .then(() => {
          let params = {
            id: this.tableData[idx].id,
            examineStatus: 1
          };
          this.axios.post(api.CENSORCHANNELCOMPANY, params).then(res => {
            if (res.data.code == 200) {
              this.$message({
                type: "success",
                message: "已发起审批"
              });
              this.searchList();
            }
          });
        })
        .catch(() => {});
    },
    //删除
    deleteItem(type, idx) {
      this.$confirm(`确定要删除当前列表吗？`, "", {
        confirmButtonText: "确定",
        cancelButtonText: "取消"
      })
        .then(() => {
          let params = {
            contractUndersignType: "1",
            id: this.tableData[idx].id
          };
          this.axios.post(api.TYPEMPLATEDELETE, params).then(res => {
            if (res.data.code == 200) {
              this.tableData.splice(idx, 1);
              this.$message({
                type: "success",
                message: "已删除"
              });
            }
          });
        })
        .catch(() => {});
    },
    //按钮显示规则
    tableBtnRule(rowData, btns) {
      let [...newbtn] = btns;
      if (rowData.examineStatus == 1) {
        newbtn = [newbtn[2]];
      } else if (rowData.examineStatus != 0 && rowData.examineStatus) {
        newbtn.pop();
      }
      return newbtn;
    },

    searchList(pageNum) {
      let param = {
        pageNum: pageNum || this.pageNum,
        pageSize: this.pageSize,
        ...this.searchForm
      };
      this.axios.post(api.CHANNELCOMPANYALLLIST, param).then(res => {
        if (res.data.code == 200) {
          let list = res.data.data.list || [];
          list.forEach(function(item, index, arr) {
            let statusArray = [
              "未合作",
              "审核中",
              "合作中",
              "审核不通过",
              //"合作终止"
            ];
            let limitsMap = {
              house: "新房",
              insurance: "保险",
              property: "产权"
            };
            let companyRepresenArray = ["否", "是"];
            let {
              house,
              insurance,
              property,
              examineStatus,
              taxrate,
              companyRepresen
            } = item;
            let limitsList = [];
            for (let key in limitsMap) {
              if (item[key] != "-1") {
                limitsList.push(limitsMap[key]);
              }
            }
            item.taxrate = !taxrate && taxrate !== 0 ? "" : taxrate + "%";
            item.statusName = statusArray[examineStatus] || "";
            item.limits = limitsList.join("，");
            item.companyRepresen = companyRepresenArray[companyRepresen] || "";
          });
          this.tableData = list;
          this.totalSize = res.data.data.total;
        }
      });
    },
    add() {
      //调用当前tabled的add
      this.$router.push({
        path: "/changeChannelOrAgent",
        query: { pageType: "add" }
      });
    },
    push() {
      //调用当前tabled的push
      this.$router.push({ path: "/importChannelOrAgent" });
    },
    handleCurrentChange() {
      this.searchList();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.searchList(1);
    }
  },
  created() {
    this.searchList();
  },
  computed: {},
  destroyed() {},
  filters: {}
};
</script>

<style lang="stylus" scoped></style>
